package com.laoxiang.modules.system.controller;

import com.laoxiang.modules.system.entity.SysConfig;
import com.laoxiang.modules.system.entity.dto.PageDTO;
import com.laoxiang.modules.system.entity.vo.SysConfigVO;
import com.laoxiang.modules.system.service.SysConfigService;
import com.laoxiang.utils.AppHttpCodeEnum;
import com.laoxiang.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * 参数配置表(SysConfig)表控制层
 *
 * @author db
 * @since 2023-11-17 20:57:19
 */
@RestController
@RequestMapping("sysConfig")
@Api(tags = "参数配置管理")
public class SysConfigController  {
    /**
     * 服务对象
     */
    @Resource
    private SysConfigService sysConfigService;

    /**
     * 分页查询所有数据
     *
     * @param pageNum 分页对象
     * @param pageSize 分页对象
     * @param sysConfigVO 查询实体
     * @return 所有数据
     */
    @ApiOperation(httpMethod = "GET", value = "参数设置列表")
    @GetMapping("/getConfigList")
    public ResponseResult getConfigList(Integer pageNum, Integer pageSize, SysConfigVO sysConfigVO){
        PageDTO pageDTO = sysConfigService.selectConfigList(pageNum, pageSize, sysConfigVO);
        return ResponseResult.okResult(pageDTO);
    }

    /**
     * 根据键名查询参数配置信息
     *
     * @param configKey 主键
     * @return 单条数据
     */
    @ApiOperation(httpMethod = "GET", value = "根据键名查询参数配置信息")
    @GetMapping("/getConfigByKey/{configKey}")
    public ResponseResult selectConfigByKey(@PathVariable String configKey){
        SysConfig sysConfig = sysConfigService.selectConfigByKey(configKey);
        return ResponseResult.okResult(sysConfig);
    }

    /**
     * 根据键id参数配置信息
     *
     * @param configId 主键
     * @return 单条数据
     */
    @ApiOperation(httpMethod = "GET", value = "根据键id获得参数配置信息")
    @GetMapping("/getConfigById/{configId}")
    public ResponseResult getConfigById(@PathVariable("configId") Long configId){
        SysConfig sysConfig = sysConfigService.selectConfigById(configId);
        return ResponseResult.okResult(sysConfig);
    }

    /**
     * 新增数据
     *
     * @param sysConfig 实体对象
     * @return 新增结果
     */
    @ApiOperation(httpMethod = "POST", value = "新增数据")
    @PostMapping("/insertConfig")
    public ResponseResult insertConfig(@RequestBody SysConfig sysConfig){
        Boolean flag = sysConfigService.checkConfigKeyUnique(sysConfig);
        if(!flag){
            return ResponseResult.errorResult(AppHttpCodeEnum.CONFIG_KEY_EXIST);
        }
        sysConfigService.insertConfig(sysConfig);
        return ResponseResult.okResult();

    }

    /**
     * 修改数据
     *
     * @param sysConfig 实体对象
     * @return 修改结果
     */
    @ApiOperation(httpMethod = "PUT", value = "修改数据")
    @PutMapping("/updateConfig")
    public ResponseResult updateConfig(@RequestBody SysConfig sysConfig){
        Boolean flag = sysConfigService.checkConfigKeyUnique(sysConfig);
        if(!flag){
            return ResponseResult.errorResult(AppHttpCodeEnum.CONFIG_KEY_EXIST);
        }
        sysConfigService.updateConfig(sysConfig);
        return ResponseResult.okResult();
    }

    /**
     * 删除数据
     *
     * @param configId 主键
     * @return 删除结果
     */
    @ApiOperation(httpMethod = "DELETE", value = "删除数据")
    @DeleteMapping("/deleteConfigById/{configId}")
    public ResponseResult deleteConfigById(@PathVariable("configId") Long configId){
        sysConfigService.deleteConfigById(configId);
        return ResponseResult.okResult();
    }

    /**
     * 批量删除数据
     *
     * @param configIds 主键列表
     * @return 删除结果
     */
    @ApiOperation(httpMethod = "DELETE", value = "批量删除数据")
    @DeleteMapping("/bachDeleteConfigById/{configIds}")
    public ResponseResult bachDeleteConfigById(@PathVariable("configIds") List<Long> configIds){
        sysConfigService.deleteConfigByIds(configIds);
        return ResponseResult.okResult();
    }
}

